﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    [Route("module")]
    public class ModuleController : Controller
    {

        private readonly IRepository<Module, Guid> _repository;
        private readonly IModuleService _moduleService; 
        private readonly IRepository<ModuleCategory, int> _mcRepository;
        public ModuleController(IRepository<Module, Guid> repository, IModuleService moduleService
            , IRepository<ModuleCategory, int> mcRepository)
        {
            _repository = repository;
            _moduleService = moduleService;
            _mcRepository = mcRepository;
        }



        [HttpGet("")]
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost("")]
        public async Task<IActionResult> Index(ModuleQueryModel model)
        {
            var data = await _moduleService.GetPaginatedResult(new GetModuleInput { Name = model.Name, CurrentPage = model.Page, MaxResultCount = model.Limit });
            return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
        }


        [HttpGet("add")]
        public IActionResult Add()
        {
            var mc = _mcRepository.GetAllList();
            ViewBag.List = mc;
            return View();
        }

        [HttpPost("add")]
        public IActionResult Add(ModuleAddAndUpdateModel model)
        {
            _repository.Insert(new Module
            {
                ModuleID = Guid.NewGuid(),
                Description = model.Description,
                ModuleType = model.ModuleType,
                Name = model.Name,
                PageKey = model.PageKey,
                PageUrl = model.PageUrl
            });

            return Json(new { success = 1, msg = "添加成功" });
        }

        [HttpGet("edit/{id}")]
        public IActionResult Edit(Guid id)
        {
            var mc = _mcRepository.GetAllList();
            ViewBag.List = mc;
            var r = _repository.FirstOrDefault(x => x.ModuleID == id);
            return View(r);
        }

        [HttpPost("edit")]
        public IActionResult Edit(ModuleAddAndUpdateModel model)
        {
            var r = _repository.FirstOrDefault(x => x.ModuleID == model.ModuleID);


            r.Description = model.Description;
            r.ModuleType = model.ModuleType;
            r.Name = model.Name;
            r.PageKey = model.PageKey;
            r.PageUrl = model.PageUrl;

            _repository.Update(r);

            return Json(new { success = 1, msg = "修改成功" });
        }


        [HttpPost("del/{id}")]
        public IActionResult Del(Guid id)
        {

            Module r = new Module { ModuleID = id };
            _repository.Delete(r);
            return Json(new { success = 1, msg = "删除成功" });
        }
    }
}
